package com.util;
import com.entity.Admin;
import com.entity.UserInfo;
import com.service.Queryables;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.beans.factory.annotation.Autowired;
public class MyAuthorizingRealm extends AuthorizingRealm {

    @Autowired
    private Queryables queryables;


      @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token)
    throws AuthenticationException {
        String username = token.getPrincipal().toString();
            Object principal = username;
             Object credentials =gets(username);
             String realmName = getName();
             ByteSource credentialsSalt = ByteSource.Util.bytes(username);
             SimpleAuthenticationInfo info = new SimpleAuthenticationInfo(principal, credentials, credentialsSalt,realmName);
            return  info;
    }

       public  String  gets( String username){
          String  ss;
           if(username.length()==5){
               UserInfo userInfo =queryables.uses(username);
               if (userInfo == null) {
                   throw new UnknownAccountException("用户不存在！");
               }
               ss   =   queryables.uses(username).getLoginPassword();
           }else {
               Admin admin =queryables.admins(username);
               if (admin == null) {
                   throw new UnknownAccountException("用户不存在！");
               }
               ss   =   queryables.admins(username).getAdmPassword();
           }
         return  ss;
       }


    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
//        String username = String.valueOf(principalCollection.getPrimaryPrincipal());
//        List<String> permissions = new ArrayList<String>();
//        permissions.add("user:updateFlight");
//        permissions.add("user:addzenjia");
//        //查到权限数据，返回授权信息(要包括 上边的permissions)
//        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
//        //将上边查询到授权信息填充到simpleAuthorizationInfo对象中
//        simpleAuthorizationInfo.addRoles(Collections.singleton("user"));
//        simpleAuthorizationInfo.addStringPermissions(permissions);
//        return simpleAuthorizationInfo;
        return null;
    }




}
